# Onjoin.tcl 2.0.4 by |nsid3r (webmaster@overtech.zzn.com)
#   Based on Onjoin Proc by tech on irc.chatnet.org
#
# Special thanks to Intel for aiding in the construction of
#   both versions 1 and 2.
# ------------------------------------------------------------
#
# Configuration:
# --------------
# This script will work as-is, no configuration is needed.
#
# Usage:
# ------
# Any person with +o in the channel or chanOp (or higher)
# access on your bot will be able to modify the onjoin.
# Type !onjoin commands in the chatroom this bot is in
# for more information.
#
# Windrop Notice!  
# ---------------
# This script will work on most channels however make sure 
# that for windrops that your bot nick and channel name
# combined is less than 64 characters long.  Otherwise,
# you will receive all sorts of weired file name errors.
#
# ============================================================
# Maintained ONLY for compatibility with OTBN 2
# bind pub - !onjoin onjoin_tcl 
# OTBN.tcl 2.0 is too old now to even bother worrying about compatibility.

# This is what makes ONJOIN work :)
bind join _ *!*@* onjoin_out
# ============================================================

proc bot-onjoin_tcl {nick addr handle chan arg5 } {
   set sparsecmd [split $arg5 " "] 
   set sendcmd [lrange $sparsecmd 1 end]

   onjoin_tcl $nick $addr $handle $chan $sendcmd
   return 0
}

proc onjoin_tcl {nick addr handle chan arg5 } {

  # Added in X-Bot 2003 SP-1 due to m.onjoin
  # -----------------------------------
  # Bot-Solutions never checked if the user was authoried before allowing use of this command.
  # Users should not be able to so much as get a debug message without being authenticated.
  if {![matchattr $handle A]} {
     puthelp "NOTICE $nick :\0034ERROR - you must authenticate before you can use this command.\003"
     return 0
  }

  if {[matchattr $handle D]} {
    putquick "NOTICE $nick :\0034ERROR - Unable To Execute Command - Access Is Disabled - Permission Denied."
    return 0
  }

  # Require ops access on channel to do anything
  if {![matchattr $handle o|o $chan]} {
     puthelp "NOTICE $nick :\0034ERROR - you can only edit the onjoin for $chan if you have at least Op access to this bot\003"
     return 0
  }

  # Make sure the user is not adding channel names to their onjoin, this causes all sorts of wacky problems
  if {[string match "*#*" $arg5]} {
     puthelp "NOTICE $nick :\0034Sorry, you may not add channel names to your onjoin.  Onjoin not updated.\003"
     return 0     
  }


  global botnick
  set counter 1
  set sparsecmd [split $arg5 " "] 
  set oncmd [lindex $sparsecmd 0]

  if { $oncmd == "" } { set oncmd "debug" }
  if { $oncmd == "erase" } { set oncmd "clear" }
  if { $oncmd == "modify" } { set oncmd "replace" }
  if { $oncmd == "edit" } { set oncmd "replace" }
  if { $oncmd == "preview" } { set oncmd "debug" }
  if { $oncmd == "help" } { set oncmd "commands" }

  switch -exact $oncmd {
    "add" {
      set fileid [open $botnick.$chan a+]
        seek $fileid 0 start
      while {![eof $fileid ]} {
        gets $fileid onjoin
        if {$counter == 1 } {
          set onjoin1 $onjoin
          if {[eof $fileid]} {
            break
          }
        } elseif {$counter == 2 } {
          set onjoin2 $onjoin
          if {[eof $fileid]} {
            break
          }
        } elseif {$counter == 3 } {
          set onjoin3 $onjoin
          if {[eof $fileid]} {
            break
          }
        } elseif {$counter == 4 } {
          set onjoin4 $onjoin
          if {[eof $fileid]} {
            break
          }
        } elseif {$counter == 5 } {
          set onjoin5 $onjoin
          if {[eof $fileid]} {
            break
          }
        } elseif {$counter == 6 } {
          set onjoin5 $onjoin
          putquick "NOTICE $nick :\0034\002 Onjoin is FULL\002\ \003"

# Error now trapped for above
#          putquick "NOTICE $nick :\0034\002 Note, if you try to add a channel name to an onjoin, you may get this message in error.  You cannot add channel names to onjoins, sorry :( \002\ \003"

          close $fileid
          return 1
        }
        incr counter
      }
      set onjoinadd [string trimleft $sparsecmd "add "] 
      set space " "
      set onjoinad1 $counter$space$onjoinadd
      puts $fileid $onjoinad1
      putquick "NOTICE $nick :\0033\002Onjoin\002\ changed.  Type \00312\002$botnick onjoin debug\002\003 to check it! \003"

# Removed since this is now trapped for above.
#      putquick "NOTICE $nick :\0034\002 Note, if you try to add a channel name to an onjoin, you may get this message in error.  The onjoin was not added because you may add channel names to onjoins, sorry :( \002\ \003"

      close $fileid
      return 1
    }
    "clear" {
      set oncl [open $botnick.$chan w+]
      putquick "NOTICE $nick :\0033\002Onjoin\002\ for $chan has been cleared! \003"
      close $oncl
    }
    "debug" {
      puthelp "NOTICE $nick :\002\0033Onjoin messages for $chan:\002\003 "

      set ondeb [open $botnick.$chan a+]
      seek $ondeb 0 start
      while {![eof $ondeb]} {
        gets $ondeb msg
        if {$msg == "" } {
          puthelp "NOTICE $nick :\002\0033End of onjoin for $chan\002\003 "
          return 0
        }
        puthelp "NOTICE $nick :$msg "
      }
    }
    "replace" {
      set onlindex [split $arg5 " " ]
      set tmp [lindex $onlindex 0]
      append tmp " "
      set oncmd [string trimleft $arg5 $tmp ]
      set onlin [lindex $onlindex 1]
      append onlin " "
      set ondel [string trimleft $oncmd $onlin ]
      set onget [open $botnick.$chan a+]
      seek $onget 0 start
      gets $onget line1
      gets $onget line2
      gets $onget line3
      gets $onget line4
      gets $onget line5
      if {$onlin == "1 "} {
        set line1 "1 "
        append line1 $ondel
      } elseif {$onlin == "2 "} {
        set line2 "2 "
        append line2 $ondel
      } elseif {$onlin == "3 "} {
        set line3 "3 "
        append line3 $ondel
      } elseif {$onlin == "4 "} {
        set line4 "4 "
        append line4 $ondel
      } elseif {$onlin == "5 "} {
        set line5 "5 "
        append line5 $ondel
      }
      close $onget
      set onget [open $botnick.$chan w+]
      puts $onget $line1
      puts $onget $line2
      puts $onget $line3
      puts $onget $line4
      puts $onget $line5
      putquick "NOTICE $nick :\0033\002Onjoin\002\ changed.  Type \00312\002$botnick onjoin debug\002\0033 to check it!\003 "

# Error trapped for above
#      putquick "NOTICE $nick :\0034\002 Note, if you try to add a channel name to an onjoin, you may get this message in error.  The onjoin was not added because you may add channel names to onjoins, sorry :( \002\ \003"
      close $onget
    }
    "commands" {
	putquick "Notice $nick :\0034This command is no longer supported.  Use (/msg) $botnick help onjoin <add|clear|debug|replace> instead."
    }
  }
}

proc onjoin_out {nick addr hand chan} {
  global botnick
  set onmsg [open $botnick.$chan a+]
  seek $onmsg 0 start
  
  while {![eof $onmsg]} {
    gets $onmsg msg1
    set tempmsg [split $msg1 " " ]
    if { [lindex $tempmsg 0] == "1" } {
      set msg [string trimleft $msg1 "1"]
    } elseif { [lindex $tempmsg 0] == "2" } {
      set msg [string trimleft $msg1 "2"]
    } elseif { [lindex $tempmsg 0] == "3" } {
      set msg [string trimleft $msg1 "3"]
    } elseif { [lindex $tempmsg 0] == "4" } {
      set msg [string trimleft $msg1 "4"]
    } elseif { [lindex $tempmsg 0] == "5" } {
      set msg [string trimleft $msg1 "5"]
    } elseif {[lindex $tempmsg 0] == ""} {
      return 1
    }
    puthelp "NOTICE $nick :$msg "
  }
  close $onmsg
}










